package com.me.forum.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.me.enums.RespType;
import com.me.forum.domain.User;
import com.me.forum.request.UserRequest;
import com.me.forum.service.impl.CategoryService;
import com.me.forum.service.impl.UserService;
import com.me.forum.utils.components.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
public class AdminUserController {
    // by Orange

    @Autowired
    private UserService userService;

//   通过id查询用户
    @GetMapping("/{id}")
    public IModel getById(@PathVariable String id, IModel model) {
        User u = userService.findById(id);
        model.success(u != null);
        //RespType.User 快捷方式 Ctrl + Shift + 空格
        model.add(RespType.User, u);
        return model;
    }
//    获取所有用户
    @GetMapping
    public IModel getAllUsers(UserRequest ureq, IModel model,String page,String size){
        IPage<User> users = userService.getUsers(ureq,page,size);
        model.success(users!= null && users.getSize() != 0);
        model.add(RespType.Users,users);
        return model;
    }
//    添加用户
    @PostMapping
    public IModel userAdd(@RequestBody UserRequest ureq,IModel model) {
        model.success(userService.addUser(ureq));
        return model;
    }
//    修改用户数据
    @PutMapping
    public IModel userUpdate(@RequestBody UserRequest ureq,IModel model) {
        model.success(userService.updateById(ureq));
        return model;
    }
//    删除用户
    @DeleteMapping
    private IModel userDelete(String id,IModel model) {
        model.success(userService.delete(id));
        return model;
    }
}
